# Maintainer: Kouhei Sutou <kou@clear-code.com>

_realname=arrow
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=20.0.0
pkgrel=3
pkgdesc="Apache Arrow is a cross-language development platform for in-memory data (mingw-w64)"
arch=(any)
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://arrow.apache.org/"
msys2_repository_url="https://github.com/apache/arrow"
msys2_references=(
  'archlinux: arrow'
  "cpe: cpe:/a:apache:arrow"
)
license=('spdx:Apache-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-abseil-cpp"
         "${MINGW_PACKAGE_PREFIX}-aws-sdk-cpp"
         "${MINGW_PACKAGE_PREFIX}-brotli"
         "${MINGW_PACKAGE_PREFIX}-bzip2"
         "${MINGW_PACKAGE_PREFIX}-c-ares"
         "${MINGW_PACKAGE_PREFIX}-double-conversion"
         "${MINGW_PACKAGE_PREFIX}-flatbuffers"
         "${MINGW_PACKAGE_PREFIX}-gflags"
         "${MINGW_PACKAGE_PREFIX}-glib2"
         "${MINGW_PACKAGE_PREFIX}-grpc"
         "${MINGW_PACKAGE_PREFIX}-libutf8proc"
         "${MINGW_PACKAGE_PREFIX}-llvm-libs"
         "${MINGW_PACKAGE_PREFIX}-lz4"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-protobuf"
         "${MINGW_PACKAGE_PREFIX}-re2"
         "${MINGW_PACKAGE_PREFIX}-snappy"
         "${MINGW_PACKAGE_PREFIX}-thrift"
         "${MINGW_PACKAGE_PREFIX}-uriparser"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-zstd")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-boost"
             "${MINGW_PACKAGE_PREFIX}-clang"
             "${MINGW_PACKAGE_PREFIX}-gobject-introspection"
             "${MINGW_PACKAGE_PREFIX}-gtk-doc"
             "${MINGW_PACKAGE_PREFIX}-llvm"
             "${MINGW_PACKAGE_PREFIX}-rapidjson"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-xsimd")
source=("https://www.apache.org/dyn/closer.lua?action=download&filename=arrow/arrow-${pkgver}/apache-arrow-${pkgver}.tar.gz"
        "https://www.apache.org/dyn/closer.lua?action=download&filename=arrow/arrow-${pkgver}/apache-arrow-${pkgver}.tar.gz.asc")
sha256sums=('89efbbf852f5a1f79e9c99ab4c217e2eb7f991837c005cba2d4a2fbd35fad212'
            'SKIP')

# See generate-valid-keys.sh
validpgpkeys=('35CF82A165DDBBA29B307B7497D7E8647AE7E47B'
              '87C072B8B6405B5780D66A3D02DABFDF1679D194'
              'F2A765669021A3D3094C200B29D94E228CAAD602'
              '6D09E881160096717426C638F105883A1735623D'
              '08D3564B7C6A9CAFBFF6A66791D18FCF079F8007'
              'E6E4AA55F38337A6EFC7A5549F453D0CC3E4F6BA'
              '265F80AB84FE03127E14F01125BCCA5220D84079'
              'E47C810A90FE21FF448DA938755E743692EA1D85'
              'D6892EA1881BD9610330AD7B0801999AF78748E8'
              'DB3D3F10215394239119F6F845127976E1E825D4'
              'DF7BAD6652219D7502C87A11CA1AB41406F9DBAD'
              '3905F254F9E504B40FFF6CF6000488D7717D3FB2'
              'B6550C65A4B9EE9F26111DB40B8A854E87467E2C'
              'BE7EF45DBAD38E4EECED390E9CBA4EF977CA20B8'
              'B90EB64A3AF15545EC8A7B8803F0D5EA3790810C'
              'AF6AADA4C9835B75973FF5DA275C532289DD0F4A')

prepare() {
  cd apache-${_realname}-${pkgver}
}

build() {
  if check_option "debug" "n"; then
    export _cmake_build_type="Release"
  else
    export _cmake_build_type="Debug"
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake.exe -Wno-dev \
      -G Ninja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCMAKE_BUILD_TYPE=${_cmake_build_type} \
      -DARROW_ACERO=ON \
      -DARROW_BUILD_UTILITIES=ON \
      -DARROW_COMPUTE=ON \
      -DARROW_CSV=ON \
      -DARROW_DATASET=ON \
      -DARROW_FILESYSTEM=ON \
      -DARROW_FLIGHT=ON \
      -DARROW_FLIGHT_SQL=ON \
      -DARROW_GANDIVA=ON \
      -DARROW_HDFS=ON \
      -DARROW_JSON=ON \
      -DARROW_PACKAGE_KIND=${MINGW_PACKAGE_PREFIX} \
      -DARROW_PACKAGE_PREFIX=${MINGW_PREFIX} \
      -DARROW_PARQUET=ON \
      -DARROW_S3=ON \
      -DARROW_THRIFT_USE_SHARED=ON \
      -DARROW_USE_GLOG=OFF \
      -DARROW_DEPENDENCY_SOURCE=SYSTEM \
      -DARROW_WITH_BROTLI=ON \
      -DARROW_WITH_BZ2=ON \
      -DARROW_WITH_LZ4=ON \
      -DARROW_WITH_SNAPPY=ON \
      -DARROW_WITH_ZLIB=ON \
      -DARROW_WITH_ZSTD=ON \
      -DBOOST_ROOT=${MINGW_PREFIX} \
      -DPARQUET_BUILD_EXECUTABLES=ON \
      -DPARQUET_REQUIRE_ENCRYPTION=ON \
      -DPython3_EXECUTABLE=${MINGW_PREFIX}/bin/python \
      -S "apache-${_realname}-${pkgver}/cpp" \
      -B "build-${MSYSTEM}-cpp"

  ${MINGW_PREFIX}/bin/cmake --build "build-${MSYSTEM}-cpp"

  declare -a _meson_extra_config
  if check_option "debug" "n"; then
    _meson_extra_config+=("--buildtype=release")
  else
    _meson_extra_config+=("--buildtype=debug")
  fi

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ${MINGW_PREFIX}/bin/meson setup \
    --prefix=${MINGW_PREFIX} \
    "${_meson_extra_config[@]}" \
    -Darrow_cpp_build_dir="${srcdir}"/build-${MSYSTEM}-cpp \
    -Darrow_cpp_build_type="${_cmake_build_type}" \
    -Dgtk_doc=true \
    "build-${MSYSTEM}-c-glib" \
    "apache-${_realname}-${pkgver}/c_glib"

  PATH="${srcdir}"/build-${MSYSTEM}-cpp/${_cmake_build_type}:$PATH \
    ${MINGW_PREFIX}/bin/meson compile -C "build-${MSYSTEM}-c-glib"
}

package() {
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install "build-${MSYSTEM}-cpp"

  PATH="${srcdir}"/build-${MSYSTEM}-c-glib/arrow-glib:"${srcdir}"/build-${MSYSTEM}-cpp/${_cmake_build_type}:$PATH \
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/meson install -C "build-${MSYSTEM}-c-glib"

  # Remove full path reference
  local _PREFIX_WIN="$(cygpath -wm ${MINGW_PREFIX})"

  for pcfile in "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/*.pc; do
    sed -s "s|${_PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${pcfile}"
  done

  for cmfile in "${pkgdir}${MINGW_PREFIX}"/lib/cmake/arrow/*.cmake; do
    sed -e "s|${_PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i "${cmfile}"
  done
}
